﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace Utils.DatabaseUtils
{
    public class TableLib
    {
        public static bool HasData( DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Tạo mới một Datatable từ một mảng các Datarow lấy từ datatable
        /// </summary>
        /// <param name="dt">Datatable chua cau truc can tao</param>
        /// <param name="dr">Danh sach mang row muon tao thanh table</param>
        /// <returns></returns>
        public static DataTable CreateTable(DataTable dt, string StrFilter)
        {
            DataRow[] rows = dt.Select(StrFilter);

            //Tao bang moi co cau truc giong bang select
            DataTable newDt = new DataTable();
            newDt = dt.Clone();

            //Chen cac row vao table moi
            IEnumerator ie = rows.GetEnumerator();
            DataRow newRow = null;
            while(ie.MoveNext())
            {
                newRow = (DataRow)ie.Current;
                newDt.ImportRow(newRow);
            }
            return newDt;
        }

        public static DataView CreateView(DataTable dt)
        {
            DataView dv = new DataView(dt);
            dv.AllowDelete = true;
            dv.AllowEdit = true;
            dv.AllowNew = true;
            return dv;
        }

        public static DataTable CreateTable(DataTable dt, string StrFilter, int MaxRowNumber)
        {

            DataTable NewDt = new DataTable();
            DataRow[] dr = dt.Select(StrFilter);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                NewDt.Columns.Add(new DataColumn(dt.Columns[i].ColumnName,dt.Columns[i].DataType));
            }

            int TempNum;
            if (dr.Length > MaxRowNumber)
            {
                for (int i = 0; i < MaxRowNumber; i++)
                {
                    DataRow newdr = NewDt.NewRow();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        newdr[j] = dr[i][j];

                    }
                    NewDt.Rows.Add(newdr);
                }
            }
            else
            {
                for (int i = 0; i < dr.Length; i++)
                {
                    DataRow newdr = NewDt.NewRow();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        newdr[j] = dr[i][j];

                    }
                    NewDt.Rows.Add(newdr);
                }
            }
            return NewDt;
        }

        public static DataTable CreateTable(DataTable dt, string StrFilter, String Order, int MaxRowNumber)
        {

            var NewDt = dt.Clone(); ;
            DataRow[] dr = dt.Select(StrFilter);

            int TempNum;
            if (dr.Length > MaxRowNumber)
            {
                for (int i = 0; i < MaxRowNumber; i++)
                {
                    DataRow newdr = NewDt.NewRow();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        newdr[j] = dr[i][j];

                    }
                    NewDt.Rows.Add(newdr);
                }
            }
            else
            {
                for (int i = 0; i < dr.Length; i++)
                {
                    DataRow newdr = NewDt.NewRow();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        newdr[j] = dr[i][j];

                    }
                    NewDt.Rows.Add(newdr);
                }
            }

            DataView dv = NewDt.DefaultView;
            dv.Sort = Order;
            return dv.ToTable();
        }

        public static DataTable CreateTable_Old(DataTable dt, string StrFilter, String Order, int MaxRowNumber)
        { 

            var NewDt = dt.Clone(); ;
            DataRow[] dr = dt.Select(StrFilter, Order);

            int TempNum;
            if (dr.Length > MaxRowNumber)
            {
                for (int i = 0; i < MaxRowNumber; i++)
                {
                    DataRow newdr = NewDt.NewRow();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        newdr[j] = dr[i][j];

                    }
                    NewDt.Rows.Add(newdr);
                }
            }
            else
            {
                for (int i = 0; i < dr.Length; i++)
                {
                    DataRow newdr = NewDt.NewRow();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        newdr[j] = dr[i][j];

                    }
                    NewDt.Rows.Add(newdr);
                }
            }

            DataView dv = NewDt.DefaultView;
            //dv.Sort = Order;
            return dv.ToTable();
        }

        /// <summary>
        /// Hàm xóa các dữ liệu trùng trong 1 bảng với cột khóa là KeyColumns có dạng như sau: DataColumn[] keyColumns = new DataColumn[]{tbl.Columns["ColumnA"], tbl.Columns["ColumnA"]};
        /// </summary>
        /// <param name="dataTable">Bảng dữ liệu cần xóa</param>
        /// <param name="keyColumns">Mảng các cột khóa căn cứ để xóa</param>
        public static DataTable TableRemoveDuplicates(DataTable dataTable, DataColumn[] keyColumns)
        {
            DataTable dt = dataTable;
            int rowNdx = 0;
            while (rowNdx < dt.Rows.Count - 1)
            {
                DataRow[] dups = FindDups(dt, rowNdx, keyColumns);
                if (dups.Length > 0)
                {
                    foreach (DataRow dup in dups)
                    {
                        dt.Rows.Remove(dup);
                    }
                }
                else
                {
                    rowNdx++;
                }
            }
            return dt;
        }

        private static DataRow[] FindDups(DataTable tbl, int sourceNdx, DataColumn[] keyColumns)
        {
            ArrayList retVal = new ArrayList();
            DataRow sourceRow = tbl.Rows[sourceNdx];
            for (int i = sourceNdx + 1; i < tbl.Rows.Count; i++)
            {
                DataRow targetRow = tbl.Rows[i];
                if (IsDup(sourceRow, targetRow, keyColumns))
                {
                    retVal.Add(targetRow);
                }
            }
            return (DataRow[])retVal.ToArray(typeof(DataRow));
        }

        private static bool IsDup(DataRow sourceRow, DataRow targetRow, DataColumn[] keyColumns)
        {
            bool retVal = true;
            foreach (DataColumn column in keyColumns)
            {
                retVal = retVal && sourceRow[column].Equals(targetRow[column]);
                if (!retVal) break;
            }
            return retVal;

        }

    }
}
